//
//  HBFeedBackCtrl.m
//  HuoBao
//
//  Created by sky on 15/12/7.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "HBFeedBackCtrl.h"
#import "HBuserFeedbackParam.h"
#import "HBuserFeedbackParser.h"
#import "NetCenter.h"
#import "HBLoginManager.h"
#import "CheckValid.h"

@interface HBFeedBackCtrl ()<UITextViewDelegate>

@property (nonatomic, strong) UITextView *contentTV;
@property (nonatomic, strong) UILabel *checkCountLable;

@end

@implementation HBFeedBackCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

-(void)buildLayout{
    self.title = @"问题吐槽";
    UIView *bgvi = [[UIView alloc]initWithFrame:CGRectMake(15, 10, SCREEN_WIDTH - 30, 430/2)];
    bgvi.backgroundColor = [LCCommon getColor:@"37383d"];
    bgvi.layer.cornerRadius = 4;
    bgvi.clipsToBounds = YES;
    [self.view addSubview:bgvi];
    
    [self addTap];
    self.contentTV = [[UITextView alloc]initWithFrame:CGRectMake(5, 0, SCREEN_WIDTH-2*15 - 10, 430/2)];
    self.contentTV.font = [UIFont systemFontOfSize:14];
    self.contentTV.layer.cornerRadius = 4;
    self.contentTV.backgroundColor = [LCCommon getColor:@"37383d"];
    self.contentTV.clipsToBounds = YES;
    self.contentTV.delegate = self;
    [bgvi addSubview:self.contentTV];
    
    [self loadEmptyTextView];
    
    self.checkCountLable = [[UILabel alloc]initWithFrame:CGRectMake(bgvi.width/2 ,bgvi.height - 15 - 10, bgvi.width/2 - 10, 14)];
    self.checkCountLable.font = [UIFont systemFontOfSize:14];
    self.checkCountLable.textColor = [LCCommon getColor:@"585858"];
    self.checkCountLable.text = @"0/140字";
    self.checkCountLable.textAlignment = NSTextAlignmentRight;
    self.checkCountLable.backgroundColor = [UIColor clearColor];

    [bgvi addSubview:self.checkCountLable];

    [self showDoneWithTitle:@"提交"];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewEditChanged:) name:UITextViewTextDidChangeNotification object:self.contentTV];
    
}


-(void)doDone{
    LLog(@"提交");
    if (self.contentTV.text.length == 0 || [self.contentTV.text isEqualToString:@"想说什么尽管说吧~"]) {
        return;
    }else if([CheckValid isIncludeEmoji:self.contentTV.text])
    {
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"吐槽不要有表情哈！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alter show];
    }else{
        [self requestToFeedBack];
    }
}



- (void)textViewEditChanged:(NSNotification *)obj
{
    UITextView *textField = (UITextView *)obj.object;
    NSString *toBeString = textField.text;
    
    
    if ([toBeString isEqualToString:@"想说什么尽管说吧~"])
    {
        self.checkCountLable.text = @"0/140字";
    }
    
    
    NSString *lang = [[textField textInputMode] primaryLanguage];
    if ([lang isEqualToString:@"zh-Hans"])
    {
        UITextRange *selectedRange = [textField markedTextRange];
        
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        
        NSString *textCount = [NSString  stringWithFormat:@"%d/140字",(int)(((140-toBeString.length)>0)? toBeString.length:140)];
        if ([textCount rangeOfString:@"-"].length>0)
        {
            textCount = @"140/140字";
        }
        self.checkCountLable.text = textCount;
        //        self.checkCountLable.text = [NSString  stringWithFormat:@"%d字",(int)(((140-toBeString.length)>0)?(140-toBeString.length):0)];
        
        if (!position)
        {
            
            if (toBeString.length >140)
            {
                textField.text = [toBeString substringToIndex:140];
                self.checkCountLable.text = @"140/140字";
                [textField resignFirstResponder];
            }
        }
    }
    else
    {
        NSString *textCount = [NSString  stringWithFormat:@"%d/140字",(int)(((140-toBeString.length)>0)? toBeString.length:140)];
        if ([textCount rangeOfString:@"-"].length>0)
        {
            textCount = @"140/140字";
        }
        self.checkCountLable.text = textCount;
        
        //        self.checkCountLable.text = [NSString  stringWithFormat:@"%d字",(int)(((140-toBeString.length)>0)?(140-toBeString.length):0)];
        
        if (toBeString.length>140)
        {
            textField.text = [toBeString substringToIndex:140];
            self.checkCountLable.text = @"140/140字";
            [textField resignFirstResponder];
        }
    }
}


- (void)loadEmptyTextView
{
    self.contentTV.text = @"想说什么尽管说吧~";
    self.checkCountLable.text = @"0/140字";
    self.contentTV.textColor = [LCCommon getColor:@"585858"];
}

- (void)loadEditedTextView
{
    self.contentTV.text = @"";
    self.contentTV.textColor = [LCCommon getColor:@"d2d2d2"];
}


-(void)doTouch:(UIGestureRecognizer *)sender{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark
#pragma mark - textViewDelegate

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@"想说什么尽管说吧~"])
    {
        [self loadEditedTextView];
    }
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@"想说什么尽管说吧~"]||[textView.text isEqualToString:@""])
    {
        [self loadEmptyTextView];
    }
    return YES;
}


#pragma mark
#pragma mark - net

- (void)requestToFeedBack
{
//    [CustomLoadingView showHudToView:nil];
    [CustomLoadingView showHudToView:self.view];
    HBuserFeedbackParam *param = [[HBuserFeedbackParam alloc]init];
    param.token = [HBLoginManager sharedHBLoginManager].currentUserInfo.token;
    param.uid = [HBLoginManager sharedHBLoginManager].currentUserInfo.uid;
    param.content = self.contentTV.text;
    HBuserFeedbackParser *parser = [[HBuserFeedbackParser alloc]init];
    [NetCenter postDataFromURL:Method_userFeedback
                         params:param
                         parser:parser
                         result:^(ParserResult *myResult)
     {
//         [CustomLoadingView hideHudForView:nil];
         [CustomLoadingView hideHudForView:self.view];
         if (myResult.code ==0)
         {
             DISSMISS_SUC(myResult.message);
             [self.navigationController popViewControllerAnimated:YES];
         }
         else
         {
             DISSMISS_ERR(myResult.message);
         }
         
     } failure:^(NSError *error) {
//         [CustomLoadingView hideHudForView:nil];
         [CustomLoadingView hideHudForView:self.view];
         DISSMISS_ERR([error description]);
     }];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
